#include <stdio.h>
#include <stdlib.h>

typedef char TElemSet;
typedef struct BinaryTreeNode *BinaryTree;
struct BinaryTreeNode {
    TElemSet data; /* 数据元素 */
    BinaryTree left; /* 左孩子指针 */
    BinaryTree right; /* 右孩子指针 */
};

BinaryTree BuildTree(TElemSet *preorder, TElemSet *inorder, int n) {
    /* 根据前序遍历序列和中序遍历序列构建有n个结点的二叉树 */
    BinaryTree tree;
    int i;

    if (n == 0) {
        tree = NULL; /* 递归终止条件，空树 */
    } else {
        /* 建立当前根结点 */
        tree = (BinaryTree)malloc(sizeof(struct BinaryTreeNode));
        tree->data = preorder[0];
        /* 在中序遍历序列中找根结点 */
        for (i = 0; i < n; i++) {
            if (inorder[i] == preorder[0])
                break;
        }
        /* 递归构建左右子树 */
        tree->left = BuildTree(preorder + 1, inorder, i);
        tree->right = BuildTree(preorder + i + 1, inorder + i + 1, n - i - 1);
    }
    return tree;
}

int Height(BinaryTree tree) {
    /* 求树的高度 */
    int h, h_left, h_right;

    if (tree == NULL) {
        h = 0; /* 递归终止条件，空树 */
    } else { /* 后序遍历 */
        h_left = Height(tree->left); /* 左子树高度 */
        h_right = Height(tree->right); /* 右子树高度 */
        h = (h_left > h_right) ? h_left : h_right;
        h ++; /* 树高为左右树高较大者加1 */
    }
    return h;
}

int main(void) {
    int n;
    TElemSet *preorder, *inorder;
    BinaryTree tree;

    scanf("%d\n", &n);
    preorder = (TElemSet *)malloc(sizeof(TElemSet) * (n + 1));
    scanf("%s\n", preorder);
    inorder = (TElemSet *)malloc(sizeof(TElemSet) * (n + 1));
    scanf("%s\n", inorder);
    tree = BuildTree(preorder, inorder, n);
    printf("%d\n", Height(tree));

    return 0;
}